package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hbpa.dao.StudentPicDao;
import com.hbpa.form.StudentPicUploadForm;
import com.hbpa.form.StudentReg;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;

public class StudentPicDaoImpl implements StudentPicDao{

	public boolean addStudentPic(String xh,String pic) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into st_pic(xh,pic) values(?,?)");
			 
			ps.setString(1, xh);
			ps.setString(2, pic); 
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}
	
	public boolean addStudentPic(StudentPicUploadForm pic) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into st_pic(xh,pic) values(?,?)");
			 
			ps.setString(1, pic.getXh());
			ps.setString(2, pic.getFileName()); 
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public String getPicByXh(String xh) {
		if(checkStudentPic(xh)==false)
		{
			return null;
		}
		
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		String pic = "";
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from st_pic where xh = ?");
			 
			ps.setString(1, xh);
			rs = ps.executeQuery();
			 
			if(rs.next())
			{  
				 pic = rs.getString("pic"); 
			}
			 return pic;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
		return null;
	}

	 
	public boolean updatStudentPic(String xh,String pic) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("update st_pic set pic=? where xh=?");
			 
			ps.setString(1, pic);
			ps.setString(2, xh); 
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	
	public  boolean checkStudentPic(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from st_pic where xh = ?");
			 
			ps.setString(1, xh);
			
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(count.trim().equals("1")){
					return true;
				}else{
					return false;
				}
			}
			 
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

}
